package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ZyzTuanDuiEntity
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.view.xrefresh.XRefreshLayout
import com.sinothk.view.xrefresh.listener.RefreshHandler
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.zyz_team_detail.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 */
class ZyzTuanDuiDetailActivity : AppTitleBaseActivity() {

    private var ziYuanZheVM: ZiYuanZheViewModel? = null

    override fun getLayoutResId(): Int = R.layout.zyz_team_detail

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
        initView()
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    private var teamId = ""
    private var teamName = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        teamId = intent.getStringExtra("id") as String
        teamName = intent.getStringExtra("name") as String

        setViewTitle("团队详情", "成员") {
            XUtils.intent().openActivity(this@ZyzTuanDuiDetailActivity, ZyzTuanDuiMemberListActivity::class.java)
                .putStringExtra("id", teamId)
                .putStringExtra("name", teamName)
                .requestCode(100)
                .start()
        }

        ziYuanZheVM = ZiYuanZheViewModel()

        initListener()

        refreshLayout.setRefreshEnable(true)
        refreshLayout.setRefreshHandler(object : RefreshHandler {
            override fun onLoadMore(refresh: XRefreshLayout?) {
            }

            override fun onRefresh(refresh: XRefreshLayout?) {
                Handler().postDelayed({
                    ziYuanZheVM!!.getZyzTuanDuiDetail(teamId)
                }, 500)
            }
        })
    }

    private fun initView() {
        ziYuanZheVM!!.getZyzTuanDuiDetail(teamId)
    }

    private fun initListener() {
        submitBtn.setOnClickListener {
            XUtils.intent().openActivity(this@ZyzTuanDuiDetailActivity, ZyzTuanDuiJoinActivity::class.java)
                .putStringExtra("id", teamId)
                .putStringExtra("title", "加入组织")
                .requestCode(100)
                .start()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ZyzTuanDuiEntity>) {
        hideLoadingDialog()
        refreshLayout.refreshComplete()
        when (result.code) {
            ResultInfo.SUCCESS -> {
                showView(result.data)
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showView(data: ZyzTuanDuiEntity) {
        teamNameTv.text = XUtils.string().getNotNullValue(data.orgName)

        activitiesNumTv.text = data.actCount.toString()
        haveNumTv.text = data.memCount.toString()
        planNumTv.text = data.plannedNumber.toString()

        linkPersonTv.text = XUtils.string().getNotNullValue(data.linkPerson)
        linkPhoneTv.text = XUtils.string().getNotNullValue(data.linkPhone)
        emailTv.text = XUtils.string().getNotNullValue(data.email)
//
        Picasso.get().load(data.filePath)
            .error(R.drawable.icon_default_img)
            .placeholder(R.drawable.icon_default_img)
            .into(imageView)

        descriptionTv.text = XUtils.string().getNotNullValue(data.description, "暂无描述 ...")


        if (data.inOrg) {
            submitBtn.visibility = View.GONE
        } else {
            submitBtn.visibility = View.VISIBLE
        }
    }
}